package marchsoft.modules.four.admin.controller;

import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import marchsoft.base.PageVO;
import marchsoft.modules.four.admin.entity.dto.RepositoryDTO;
import marchsoft.modules.four.admin.entity.dto.RepositoryDTOH5;
import marchsoft.modules.four.admin.service.ReService;
import marchsoft.modules.four.common.entity.Repository;
import marchsoft.response.Result;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.core.io.Resource;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

@RequestMapping("/api")
@RestController
@RequiredArgsConstructor
public class ReController {

    private final ReService service;

    @ApiOperation("分页条件查询题库")
    @GetMapping("/eRepository")
    public Result<Object> selectAllRe(PageVO pageVO, RepositoryDTO repositoryDTO){
        return Result.success(service.queryAll1(pageVO,repositoryDTO));
    }

//    @ApiOperation("查询安全分类名称")
//    @GetMapping("/eSafeType/allName")
//    public Result<Object> selectAllSafeTypeName( ){
//        return Result.success(service.selectAllName());
//    }

    @ApiOperation("导出数据")
    @GetMapping("/eRepository/download")
    public void doExport(HttpServletResponse response, RepositoryDTOH5 repositoryDTOH5) throws Exception{
        service.download(service.queryAll(repositoryDTOH5), response);
    }

    @ApiOperation("根据id删除")
    @DeleteMapping("/eRepository")
    public Result<Object> delete(@RequestBody List<Integer> ids ){
        service.deleteRe(ids);
        return Result.success();
    }

    @ApiOperation("新增题库")
    @PostMapping("eRepository")
    public Result<Object> insert(@RequestBody Repository repository){
        service.insertRE(repository);
        return Result.success();
    }

    @ApiOperation("更新题库")
    @PutMapping("/eRepository")
    public Result<Object> update(@RequestBody Repository repository){
        service.updateRe(repository);
        return Result.success();
    }


    @ApiOperation(value = "批量导入试题")
    @PostMapping(value = "/eRepository/import")
    public Result<Void> importData(@RequestParam(name = "file") MultipartFile file, HttpServletResponse res) {
        service.importRepository(file, res);
        return Result.success();
    }

    @ApiOperation("下载模板")
    @GetMapping(value = "/eRepository/downloadTemplate")
    public Result<Void> downloadTemplate(HttpServletResponse response) throws Exception {
        String fileName = "repositoryInfoTemplate.xlsx";
        String path = "template/excel/";
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources(path + fileName);
        Resource resource = resources[0];
        //获得文件流，因为在jar文件中，不能直接通过文件资源路径拿到文件，但是可以在jar包中拿到文件流
        InputStream stream = resource.getInputStream();
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        // 响应类型,编码
        response.setContentType("application/octet-stream;charset=UTF-8");
        // 形成输出流
        OutputStream osOut = response.getOutputStream();
        copyStream(stream, osOut, true);
        // 刷新此输出流并强制将所有缓冲的输出字节被写出
        osOut.flush();
        // 关闭流
        osOut.close();
        return Result.success();
    }

    public static void copyStream(InputStream ips, OutputStream ops, boolean closeStream) throws Exception {
        byte[] buf = new byte[1024];
        int len = ips.read(buf);
        while (len != -1) {
            ops.write(buf, 0, len);
            len = ips.read(buf);
        }
    }



}
